<!-- 限购设置 -->

<template>
  <el-form
    ref="formRef"
    :model="form.data"
    :disabled="form.status==='r'||form.submitting"
    :label-width="form.show&&isActive?'auto':undefined"
    class="commodity-form-module"
  >
    <div class="integrated">
      <!-- 规则：必选, 限购模式在三种形式 -->
      <!-- 不限购, 用户不需要输入，清除purchaseQuantiyAtMost字段 -->
      <!-- 按用户id限购, 用户需要输入 -->
      <!-- 按订单限购, 用户需要输入 -->
      <el-form-item
        label="限购模式"
        prop="limitType"
        verify
      >
        <el-select
          style="width: 100%"
          v-model="form.data.limitType"
          placeholder="请选择限购模式"
          @change="
              value => {
                  if (value === 0) {
                      $set(
                          form.data,
                          'purchaseQuantiyAtMost',
                          undefined
                      )
                  }
              }
          "
        >
          <el-option
            v-for="item in $enum.ENUM_COMMODITY_LIMIT_TYPE"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <!-- 规则：必选，限购数量 1. 只能输入int 2. 大小范围：1 ~ 1000000 -->
      <!-- 关联影响：限购数量变化会重新校验起购数量 -->
      <el-form-item
        v-if="showLimitBugNum"
        label="限购数量"
        prop="purchaseQuantiyAtMost"
        verify
        watch="purchaseQuantiyAtLeast"
      >
        <SafeInputNumber
          style="width: 100%"
          v-model="form.data.purchaseQuantiyAtMost"
          :min="1"
          :max="1000000"
          :precision="0"
          placeholder="请输入限购数量，范围1~1000000"
        />
      </el-form-item>
    </div>
    <!-- 规则：必选，起购数量 1. 只能输入int 2. 大小范围：1 ~ 1000000 3. 起购数量不得大于限购数量 -->
    <el-form-item
      label="起购数量"
      prop="purchaseQuantiyAtLeast"
      verify
      :rules="{
          trigger: ['blur', 'change'],
          validator: (rule, value, callback) => {
              // 如果purchaseQuantiyAtMost不存在直接跳过检查
              if (!form.data.purchaseQuantiyAtMost) {
                  callback()
              }
              // 1. 起购数量不得大于限购数量
              if (
                  parseFloat(value) >
                  parseFloat(
                      form.data.purchaseQuantiyAtMost
                  )
              ) {
                  return callback(
                      '起购数量不得大于限购数量'
                  )
              }
              callback()
          }
      }"
    >
      <SafeInputNumber
        style="width: 100%"
        v-model="form.data.purchaseQuantiyAtLeast"
        :min="1"
        :max="1000000"
        :precision="0"
        placeholder="请输入起购数量，范围1~1000000"
      />
    </el-form-item>
  </el-form>
</template>

<script>
import mixin from './mixin'

export default {
  name: 'PurchaseLimitationSettings',
  mixins: [mixin],
  data () {
    return {}
  },
  methods: {}
}
</script>

<style lang="scss" scoped>

</style>
